package com.HuangJiaQing.www.controller;

import com.HuangJiaQing.www.model.Goods;
import com.HuangJiaQing.www.model.Marketers;
import com.HuangJiaQing.www.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author HONOR
 * @date 2022/7/28 23:19
 */
@Controller
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @RequestMapping(value = "/toShelves", method = RequestMethod.GET)
    public String toShelves() {
        return "shelves";
    }

    @RequestMapping(value = "/shelves", method = RequestMethod.POST)
    public String addGoods(Goods goods, MultipartFile photo, HttpSession session) {
        Marketers marketers = (Marketers) session.getAttribute("marketers");
        String s = goodsService.fileUp(photo);
        goods.setAvatar(s);
        goods.setMarketersId(marketers.getMarketersId());
        goods.setMarketersName(marketers.getMarketersName());
        goodsService.insertGoods(goods);
        return "redirect:/marketersLogin";
    }

    @RequestMapping(value = "/toAddList", method = RequestMethod.GET)
    public String toAddList() {
        return "addList";
    }

    @RequestMapping(value = "/addListGoods", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public String addListGoods(@RequestBody List<Goods> goodsList, HttpSession session) {
        Marketers marketers = (Marketers) session.getAttribute("marketers");
        for (Goods goods : goodsList) {
            goods.setMarketersId(marketers.getMarketersId());
            goods.setMarketersName(marketers.getMarketersName());
            goodsService.insertGoods(goods);
        }
        return "redirect:/marketersLogin";
    }


    @RequestMapping(value = "/goodsSearch", method = RequestMethod.GET)
    public String searchGoods(Model model, String search) {
        List<Goods> goods = goodsService.searchGoodsAndMarketers(search);
        model.addAttribute("goods", goods);
        return "search";
    }

    @RequestMapping(value = "/selectGoods/{id}", method = RequestMethod.GET)
    public String selectGoodsByMarketersId(Model model, @PathVariable("id") Integer id) {
        List<Goods> goods = goodsService.selectGoodsByMarketersId(id);
        model.addAttribute("goods", goods);
        return "goods";
    }

    @RequestMapping(value = "/toAdminGoods/{id}", method = RequestMethod.GET)
    public String toAdminGoods(@PathVariable("id") Integer id, Model model) {
        List<Goods> goods = goodsService.selectGoodsByMarketersId(id);
        model.addAttribute("goods", goods);
        return "adminGoods";
    }

    @RequestMapping(value = "/deleteGoods/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public String deleteGoods(@PathVariable("id") Integer id) {
        goodsService.deleteGoodsById(id);
        return "success";
    }

    @RequestMapping(value = "/deleteAllGoods")
    @ResponseBody
    public String deleteAllGoods(String gid) {
        String[] split = gid.split(",");
        for (String s : split) {
            int id = Integer.parseInt(s);
            goodsService.deleteGoodsById(id);
        }
        return "success";
    }

    @RequestMapping(value = "/forceDelete/{id}", method = RequestMethod.DELETE)
    public String forceDelete(@PathVariable("id") Integer id) {
        int i = goodsService.forceDeleteGoods(id);
        return "redirect:/toAdminGoods/" + i;
    }


    @RequestMapping(value = "/toUpdateGoods/{id}", method = RequestMethod.GET)
    public String toUpdateGoods(@PathVariable("id") Integer id, Model model) {
        Goods goods = goodsService.selectGoodsById(id);
        model.addAttribute("goods", goods);
        return "updateGoods";
    }

    @RequestMapping("/updateGoods")
    public String updateGoods(Goods goods, MultipartFile photo) {
        if (!photo.isEmpty()) {
            String s = goodsService.fileUp(photo);
            goods.setAvatar(s);
        }
        goodsService.updateGoodsSelective(goods);
        return "redirect:/marketersLogin";
    }
}
